/**
 * Title : jFamilyTree
 * Description: jFamilyTree
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.commons;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

/**
 * Export dialog
 * 
 * @author Bernard Bou
 */
public class XslExportDialog extends JDialog
{
	static private final long serialVersionUID = 1L;

	static private String[] theViewOutputs = { "view:xml", "view:html", "view:text" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

	// V A L U E S

	/**
	 * Properties (input/output)
	 */
	protected Properties theProperties;

	/**
	 * Ok result
	 */
	public boolean ok;

	// C O M P O N E N T S

	/**
	 * Export text
	 */
	private JComboBox theExportComboBox;

	/**
	 * XSL text
	 */
	private JComboBox theXslComboBox;

	/**
	 * Data panel
	 */
	protected JPanel theDataPanel;

	// C O N S T R U C T O R

	/**
	 * Constructor
	 * 
	 * @param theseProperties
	 *        settings
	 */
	public XslExportDialog(Properties theseProperties)
	{
		super();
		theProperties = theseProperties;

		List<String> thesePresetXsls = new ArrayList<String>();
		for (String thisUrl : Searcher.findFileUrls(".*jFamilyTree2.*\\.xsl$")) //$NON-NLS-1$
			thesePresetXsls.add(thisUrl);

		String theXSLsProperty = theProperties.getProperty("exportxsls"); //$NON-NLS-1$
		if (theXSLsProperty != null)
		{
			String[] theseProviders = theXSLsProperty.split(":"); //$NON-NLS-1$
			for (String thisProvider : theseProviders)
				thesePresetXsls.add(thisProvider);
		}

		initialize(thesePresetXsls, theViewOutputs);
	}

	/**
	 * Initialize
	 */
	@SuppressWarnings("serial")
	protected void initialize(List<String> thesePresetXsls, String[] theseViewOutputs)
	{
		setTitle(Messages.getString("XslExportDialog.0")); //$NON-NLS-1$
		setResizable(true);

		// images
		Icon thisIcon = new ImageIcon(XslExportDialog.class.getResource("images/xsl.png")); //$NON-NLS-1$
		JLabel thisHeaderLabel = new JLabel();
		thisHeaderLabel.setIcon(thisIcon);
		thisHeaderLabel.setVerticalTextPosition(JLabel.TOP);
		thisHeaderLabel.setHorizontalTextPosition(JLabel.CENTER);
		thisHeaderLabel.setFont(new Font("Dialog", Font.BOLD, 16)); //$NON-NLS-1$
		thisHeaderLabel.setText(Messages.getString("XslExportDialog.1")); //$NON-NLS-1$
		thisHeaderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

		// labels
		JLabel thisXmlLabel = new JLabel(Messages.getString("XslExportDialog.10")); //$NON-NLS-1$
		JLabel thisXslLabel = new JLabel(Messages.getString("XslExportDialog.11")); //$NON-NLS-1$

		// text
		ListCellRenderer thisRenderer = new DefaultListCellRenderer()
		{
			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
			 */
			@Override
			public Component getListCellRendererComponent(JList thisList, Object thisValue, int thisIndex, boolean thisIsSelected, boolean thisCellHasFocus)
			{
				String thisString = (String) thisValue;
				if (thisString != null)
				{
					int thisPosition = thisString.lastIndexOf('/');
					if (thisPosition != -1)
						thisString = thisString.substring(thisPosition + 1);
				}
				return super.getListCellRendererComponent(thisList, thisString, thisIndex, thisIsSelected, thisCellHasFocus);
			}
		};
		theExportComboBox = new JComboBox(theseViewOutputs);
		theExportComboBox.setEditable(true);
		theExportComboBox.setRenderer(thisRenderer);
		theExportComboBox.setPreferredSize(new Dimension(300, 20));
		theXslComboBox = new JComboBox();
		theXslComboBox.setEditable(true);
		theXslComboBox.setRenderer(thisRenderer);
		theXslComboBox.setPreferredSize(new Dimension(300, 20));
		for (String thisItem : thesePresetXsls)
			theXslComboBox.addItem(thisItem);

		// tooltips
		theExportComboBox.setToolTipText(Messages.getString("XslExportDialog.12")); //$NON-NLS-1$
		theXslComboBox.setToolTipText(Messages.getString("XslExportDialog.13")); //$NON-NLS-1$

		// buttons
		JButton thisXmlBrowseButton = new JButton(Messages.getString("XslExportDialog.15")); //$NON-NLS-1$
		JButton thisXslBrowseButton = new JButton(Messages.getString("XslExportDialog.15")); //$NON-NLS-1$
		JButton thisOKButton = new JButton(Messages.getString("XslExportDialog.20")); //$NON-NLS-1$
		JButton thisCancelButton = new JButton(Messages.getString("XslExportDialog.21")); //$NON-NLS-1$

		// panels
		theDataPanel = new JPanel();
		theDataPanel.setLayout(new GridBagLayout());
		theDataPanel.add(thisXslLabel, new GridBagConstraints(0, 0, 1, 1, 0., 0., GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 0, 0), 0, 0));
		theDataPanel.add(thisXmlLabel, new GridBagConstraints(0, 2, 1, 1, 0., 0., GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 0, 0), 0, 0));
		theDataPanel.add(theXslComboBox, new GridBagConstraints(0, 1, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 10), 0, 0));
		theDataPanel.add(theExportComboBox, new GridBagConstraints(0, 3, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 10), 0, 0));
		theDataPanel.add(thisXslBrowseButton, new GridBagConstraints(1, 1, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 0, 20), 0, 0));
		theDataPanel.add(thisXmlBrowseButton, new GridBagConstraints(1, 3, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 0, 20), 0, 0));

		JPanel thisButtonPanel = new JPanel();
		thisButtonPanel.setLayout(new FlowLayout());
		thisButtonPanel.add(thisCancelButton);
		thisButtonPanel.add(thisOKButton);

		// action
		thisXmlBrowseButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				String thisFolder = FileDialogs.getXmlUrl(theProperties.getProperty("base", ".")); //$NON-NLS-1$ //$NON-NLS-2$
				if (thisFolder != null && !thisFolder.isEmpty())
					theExportComboBox.getEditor().setItem(thisFolder);
			}
		});
		thisXslBrowseButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				String thisFolder = FileDialogs.getXmlUrl(theProperties.getProperty("base", ".")); //$NON-NLS-1$ //$NON-NLS-2$
				if (thisFolder != null && !thisFolder.isEmpty())
					theXslComboBox.getEditor().setItem(thisFolder);
			}
		});
		thisOKButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				ok = true;
				setVisible(false);
			}
		});
		thisCancelButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				setVisible(false);
			}
		});

		// assemble
		JPanel thisPanel = new JPanel();
		thisPanel.setLayout(new BoxLayout(thisPanel, BoxLayout.Y_AXIS));
		thisPanel.add(Box.createRigidArea(new Dimension(0, 10)));
		thisPanel.add(thisHeaderLabel);
		thisPanel.add(theDataPanel);
		thisPanel.add(thisButtonPanel);
		thisPanel.add(Box.createRigidArea(new Dimension(0, 10)));

		setContentPane(thisPanel);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setVisible(boolean)
	 */
	public void setVisible(boolean thisFlag)
	{
		if (thisFlag)
		{
			ok = false;

			// read properties into components
			String thisExportValue = theProperties.getProperty("exporturl"); //$NON-NLS-1$
			theExportComboBox.addItem(thisExportValue);
			theExportComboBox.getEditor().setItem(thisExportValue);

			String thisXslValue = theProperties.getProperty("exportxsl"); //$NON-NLS-1$
			theXslComboBox.addItem(thisXslValue);
			theXslComboBox.getEditor().setItem(thisXslValue);

			pack();
			Utils.center(this);
		}
		else
		{
			if (ok)
			{
				// update properties from components
				theProperties.setProperty("exporturl", (String) theExportComboBox.getEditor().getItem()); //$NON-NLS-1$
				theProperties.setProperty("exportxsl", (String) theXslComboBox.getEditor().getItem()); //$NON-NLS-1$
			}
		}
		super.setVisible(thisFlag);
	}
}
